package com.jeff.tianti.controller;

import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.cms.entity.AwardRecord;
import com.jeff.tianti.cms.service.AwardRecordService;
import com.jeff.tianti.common.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.jeff.tianti.common.dto.AjaxResult;

/**
 * 文件异步上传Controller
 *
 * @author JeffXu
 * @since 2016-03-14
 */
@Controller
@RequestMapping("/upload")
public class UploadController {

    public final static String ATTACH_SAVE_PATH = "attach";
    @Value("#{configProperties['image.upload.path']}")
    String uploadImgPath;

    @Autowired
    AwardRecordService awardRecordService;

    @RequestMapping("/uploadAttach")
    public void uploadAttach(HttpServletRequest request, PrintWriter out) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        MultipartFile multipartFile = null;
        String fileName = null;
        for (Map.Entry<String, MultipartFile> set : fileMap.entrySet()) {
            multipartFile = set.getValue();// 文件名
        }
        fileName = this.storeIOc(multipartRequest, multipartFile);

        out.print(fileName);
    }


    @RequestMapping("/ajax/upload_file")
    @ResponseBody
    public AjaxResult ajaxUploadFile(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {

            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            MultipartFile multipartFile = null;
            String fileName = null;
            for (Map.Entry<String, MultipartFile> set : fileMap.entrySet()) {
                multipartFile = set.getValue();// 文件名
            }
            fileName = this.storeIOc(multipartRequest, multipartFile);

            ajaxResult.setData(fileName);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @RequestMapping("/ajax/upload_excel")
    @ResponseBody
    public AjaxResult ajaxUploadExcel(HttpServletRequest request)throws Exception {
        String columnId = request.getParameter("columnId");
        String awardTimeStr = request.getParameter("awardTime");
        Date awardTime=null;
        if (StringUtils.isNotBlank(awardTimeStr)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            awardTime = sdf.parse(awardTimeStr);
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            MultipartFile multipartFile = null;
            String fileName = null;
            for (Map.Entry<String, MultipartFile> set : fileMap.entrySet()) {
                multipartFile = set.getValue();// 文件名
            }
            InputStream inputStream = multipartFile.getInputStream();
            List<List<String>> awardRecordList = ExcelUtils.readXlsx(inputStream, 1);
            for (int i = 0; i < awardRecordList.size(); i++) {
                List<String> awardRecordSingle = awardRecordList.get(i);
                AwardRecord awardRecord = new AwardRecord();
                if (!CollectionUtils.isEmpty(awardRecordSingle)){
                    for (int j = 0; j < awardRecordSingle.size(); j++) {
                        if (j==0){
                            awardRecord.setName(awardRecordSingle.get(j));//作品名称
                        }
                        if (j==1){
                            awardRecord.setCompany(awardRecordSingle.get(1));//获奖单位
                        }

                        if (j==2){
                            awardRecord.setLevel(awardRecordSingle.get(2));//获奖单位
                        }
                        awardRecord.setColumnId(columnId);
                        awardRecord.setAwardTime(awardTime);
                    }
                    awardRecordService.save(awardRecord);
                }
            }

            ajaxResult.setData(fileName);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    // 接受图片，返回文件地址
    private String storeIOc(HttpServletRequest request, MultipartFile file) {
        String result = "";
        String realPath = request.getSession().getServletContext().getRealPath("uploads");
        if (file == null) {
            return null;
        }
        if (StringUtils.isNotBlank(uploadImgPath)) {
            realPath = uploadImgPath;
        }
        String fileName = "";
        String logImageName = "";
        if (file.isEmpty()) {
            result = "文件未上传";
        } else {
            String _fileName = file.getOriginalFilename();
            String suffix = _fileName.substring(_fileName.lastIndexOf("."));
            if (StringUtils.isNotBlank(suffix)) {
                if (suffix.equalsIgnoreCase(".xls") || suffix.equalsIgnoreCase(".xlsx") || suffix.equalsIgnoreCase(".txt") || suffix.equalsIgnoreCase(".png")
                        || suffix.equalsIgnoreCase(".doc") || suffix.equalsIgnoreCase(".docx") || suffix.equalsIgnoreCase(".pdf")
                        || suffix.equalsIgnoreCase(".ppt") || suffix.equalsIgnoreCase(".pptx") || suffix.equalsIgnoreCase(".gif")
                        || suffix.equalsIgnoreCase(".jpg") || suffix.equalsIgnoreCase(".jpeg") || suffix.equalsIgnoreCase(".bmp")) {
                    // /**使用UUID生成文件名称**/
                    logImageName = UUID.randomUUID().toString() + suffix;

                    fileName = realPath + File.separator + ATTACH_SAVE_PATH + File.separator + logImageName;
                    File restore = new File(fileName);
                    try {
                        file.transferTo(restore);
                        result = "/uploads/attach/" + logImageName;
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    result = "文件格式不对，只能上传ppt、ptx、doc、docx、xls、xlsx、pdf、png、jpg、jpeg、gif、bmp格式";
                }
            }
        }
        return result;
    }

}